package sort;

import java.util.ArrayDeque;
import java.util.Deque;

public class SortReview {
    public static void show(int[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length; ++i) {
            System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ", ");
        }
        System.out.print("]\n");
    }

    private static void swap(int[] arr, int i, int j) {
        if (i != j) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }

    // 选择排序
    public static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length; ++i) {
            int minI = i;
            for (int j = i + 1; j < arr.length; ++j) {
                if (arr[j] < arr[minI]) {
                    minI = j;
                }
            }
            swap(arr, minI, i);
        }
    }

    // 堆排序
    public static void heapSort(int[] arr) {
        int len = arr.length;
        createHeap(arr);
        while (len > 1) {
            swap(arr, 0, len-- - 1);
            shiftDown(arr,0, len);
        }
    }

    private static void createHeap(int[] arr) {
        int size = arr.length;
        for (int parent = (size - 1 - 1) / 2; parent >= 0; --parent) {
            shiftDown(arr, parent, size);
        }
    }

    private static void shiftDown(int[] arr, int parent, int len) {
        int child = parent * 2 + 1;
        while (child < len) {
            if (child + 1 < len && arr[child] < arr[child + 1]) {
                child++;
            }
            if (arr[child] > arr[parent]) {
                swap(arr, child, parent);
                parent = child;
                child = parent * 2 + 1;
            } else {
                break;
            }
        }
    }

    // 冒泡排序
    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; ++i) {
            boolean flag = true;
            for (int j = 0; j < arr.length - i - 1; ++j) {
                if (arr[j] > arr[j + 1]) {
                    flag = false;
                    swap(arr, j + 1, j);
                }
            }
            if (flag) {
                break;
            }
        }
    }

    // 插入排序
    public static void insertSort(int[] arr) {
        for (int i = 0; i < arr.length; ++i) {
            int temp = arr[i];
            int j = i - 1;
            for (; j >= 0; --j) {
                if (arr[j] > temp) {
                    arr[j + 1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + 1] = temp;
        }
    }

    // 快速排序
    public static void quickSort(int[] arr) {
        quickSort(arr, 0, arr.length - 1);
    }

    public static void quickSort(int[] arr, int left, int right) {
        Deque<Integer> s = new ArrayDeque<>();
        int pivot = partition(arr, left, right);
        if (pivot > left + 1) {
            s.push(left);
            s.push(pivot - 1);
        }
        if (pivot < right - 1) {
            s.push(pivot + 1);
            s.push(right);
        }

        while (!s.isEmpty()) {
            right = s.pop();
            left = s.pop();
            pivot = partition(arr, left, right);
            if (pivot > left + 1) {
                s.push(left);
                s.push(pivot - 1);
            }
            if (pivot < right - 1) {
                s.push(pivot + 1);
                s.push(right);
            }
        }
    }

//    private static int getPivot(int[] arr, int left, int right) {
//        int mid = (left + right) / 2;
//        int min = arr[left];
//        int max = arr[left];
//        if (arr[mid] < min) {
//            min = arr[mid];
//        }
//        if (arr[right] < min) {
//            min = arr[right];
//        }
//        if (arr[mid] > max) {
//            max = arr[mid];
//        }
//        if (arr[right] > max) {
//            max = arr[right];
//        }
//        return arr[left] + arr[right] + arr[mid] - min - max;
//    }

    private static int partition(int[] arr, int left, int right) {
        int pivot = arr[left];
        while (left < right) {
            while (left < right && arr[right] >= pivot) {
                right--;
            }
            arr[left] = arr[right];
            while (left < right && arr[left] <= pivot) {
                left++;
            }
            arr[right] = arr[left];
        }
        arr[left] = pivot;
        return left;
    }

    // 希尔排序
    public static void shellSort(int[] arr) {
        int gap = arr.length;
        while (gap > 1) {
            gap /= 2;
            shell(arr, gap);
        }
    }

    private static void shell(int[] arr, int gap) {
        for (int i = gap; i < arr.length; ++i) {
            int temp = arr[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (arr[j] > temp) {
                    arr[j + gap] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + gap] = temp;
        }
    }

    // 归并排序
    public static void mergeSort(int[] arr) {
        mergeSort(arr, 0, arr.length - 1);
    }

    private static void mergeSort(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        int mid = (left + right) / 2;
        mergeSort(arr, left, mid);
        mergeSort(arr, mid + 1, right);
        merge(arr, left, mid, right);
    }

    private static void merge(int[] arr, int left, int mid, int right) {
        int l1 = left;
        int l2 = mid + 1;
        int k = 0;
        int[] temp = new int[right - left + 1];
        while (l1 <= mid && l2 <= right) {
            if (arr[l1] <= arr[l2]) {
                temp[k++] = arr[l1++];
            } else {
                temp[k++] = arr[l2++];
            }
        }
        while (l1 <= mid) {
            temp[k++] = arr[l1++];
        }
        while (l2 <= right) {
            temp[k++] = arr[l2++];
        }

        for (int i = 0; i < temp.length; ++i) {
            arr[left + i] = temp[i];
        }
    }
}
